const express = require("express");
const app = express();

const PORT = process.env.PORT || 5008;


// 映射public目录中的静态资源
const path = require("path");
const staticRoot = path.resolve(__dirname, "../public");
app.use(express.static(staticRoot));


// 应用token中间件
app.use(require("./tokenMiddleware"));

// 解析 application/x-www-form-urlencoded 格式的请求体
app.use(express.urlencoded({ extended: true }));

// 解析 application/json 格式的请求体
app.use(express.json());

// 应用日志中间件
app.use(require("./apiLoggerMid"));


// 处理 api 的请求
app.use("/api/user", require("./api/user"));
app.use("/api/record", require("./api/record"));
app.use("/api/sts", require("./api/sts"));

// 处理错误的中间件
app.use(require("./errorMiddleware"));

// 启动服务器
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});
